package com.maple.retry.retrytask.convertor;

import com.maple.retry.entity.RetryTaskDO;
import com.maple.retry.entity.RetryTaskHistoryDO;
import com.maple.retry.retrytask.enums.RetryTaskStatusEnum;
import com.maple.retry.retrytask.model.RetryTaskInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * @author: maple
 * @version: RetryTaskInfoConverter.java, v 0.1 2020年12月01日 19:30 maple Exp $
 */
public class RetryTaskInfoConvertor {

    public static RetryTaskDO convert2RetryTaskDO(RetryTaskInfo taskInfo) {

        if (taskInfo == null) {
            return null;
        }

        RetryTaskDO taskDO = new RetryTaskDO();
        taskDO.setTaskId(taskInfo.getId());
        taskDO.setBizId(taskInfo.getBizId());
        taskDO.setBizType(taskInfo.getBizType());
        taskDO.setBizContext(taskInfo.getBizContext());
        taskDO.setStatus(taskInfo.getStatus().getCode());
        taskDO.setGmtCreate(taskInfo.getGmtCreate());
        taskDO.setGmtModified(taskInfo.getGmtModified());
        taskDO.setScheduleTime(taskInfo.getScheduleTime());
        taskDO.setRetryNo(String.valueOf(taskInfo.getRetryNo()));
        taskDO.setMemo(StringUtils.defaultIfBlank(taskInfo.getMemo(), ""));

        return taskDO;
    }

    /**
     * 将归档的补偿任务DO转化为归档补偿任务模型
     *
     * @param taskHistoryDO 归档的补偿任务DO
     * @return 补偿任务模型
     */
    public static RetryTaskInfo convert2RetryTaskInfo(RetryTaskHistoryDO taskHistoryDO) {
        if (taskHistoryDO == null) {
            return null;
        }

        RetryTaskInfo retryTaskInfo = new RetryTaskInfo();
        retryTaskInfo.setId(taskHistoryDO.getTaskId());
        retryTaskInfo.setBizId(taskHistoryDO.getBizId());
        retryTaskInfo.setBizType(taskHistoryDO.getBizType());
        retryTaskInfo.setBizContext(taskHistoryDO.getBizContext());
        retryTaskInfo.setStatus(RetryTaskStatusEnum.getByCode(taskHistoryDO.getStatus()));
        retryTaskInfo.setGmtCreate(taskHistoryDO.getGmtCreate());
        retryTaskInfo.setGmtModified(taskHistoryDO.getGmtModified());
        retryTaskInfo.setScheduleTime(taskHistoryDO.getScheduleTime());
        try {
            retryTaskInfo.setRetryNo(Integer.parseInt(taskHistoryDO.getRetryNo()));
        } catch (NumberFormatException efe) {
            retryTaskInfo.setRetryNo(0);
        }
        retryTaskInfo.setMemo(taskHistoryDO.getMemo());

        return retryTaskInfo;
    }

    /**
     * 将凭证DO转化为补偿任务模型
     *
     * @param retryTaskDO 补偿任务DO
     * @return 补偿任务模型
     */
    public static RetryTaskInfo convert2RetryTaskInfo(RetryTaskDO retryTaskDO) {
        if (retryTaskDO == null) {
            return null;
        }

        RetryTaskInfo retryTaskInfo = new RetryTaskInfo();
        retryTaskInfo.setId(retryTaskDO.getTaskId());
        retryTaskInfo.setBizId(retryTaskDO.getBizId());
        retryTaskInfo.setBizType(retryTaskDO.getBizType());
        retryTaskInfo.setBizContext(retryTaskDO.getBizContext());
        retryTaskInfo.setStatus(RetryTaskStatusEnum.getByCode(retryTaskDO.getStatus()));
        retryTaskInfo.setGmtCreate(retryTaskDO.getGmtCreate());
        retryTaskInfo.setGmtModified(retryTaskDO.getGmtModified());
        retryTaskInfo.setScheduleTime(retryTaskDO.getScheduleTime());
        try {
            retryTaskInfo.setRetryNo(Integer.parseInt(retryTaskDO.getRetryNo()));
        } catch (NumberFormatException efe) {
            retryTaskInfo.setRetryNo(0);
        }

        retryTaskInfo.setMemo(retryTaskDO.getMemo());

        return retryTaskInfo;
    }

    /**
     * 将补偿任务模型转化为归档补偿任务DO
     *
     * @param taskInfo 补偿任务模型
     * @return 归档补偿任务DO
     */
    public static RetryTaskHistoryDO convert2RetryTaskHistoryDO(RetryTaskInfo taskInfo) {
        if (taskInfo == null) {
            return null;
        }

        RetryTaskHistoryDO taskHistoryDO = new RetryTaskHistoryDO();
        taskHistoryDO.setTaskId(taskInfo.getId());
        taskHistoryDO.setBizId(taskInfo.getBizId());
        taskHistoryDO.setBizType(taskInfo.getBizType());
        taskHistoryDO.setBizContext(taskInfo.getBizContext());
        taskHistoryDO.setStatus(taskInfo.getStatus().getCode());
        taskHistoryDO.setGmtCreate(taskInfo.getGmtCreate());
        taskHistoryDO.setGmtModified(taskInfo.getGmtModified());
        taskHistoryDO.setScheduleTime(taskInfo.getScheduleTime());
        taskHistoryDO.setRetryNo(String.valueOf(taskInfo.getRetryNo()));
        taskHistoryDO.setMemo(StringUtils.defaultIfBlank(taskInfo.getMemo(), ""));

        return taskHistoryDO;
    }
}
